package com.utils;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateHelper {

    /**
     * 获取上传图片名
     * @return
     */
    public static String getImageName(){
        SimpleDateFormat  sdf= new SimpleDateFormat("yyyyMMddHHmmssS");
        return sdf.format(new Date());
    }

    /**
     * 对date日期进行加减操作
     * @param date 一个日期
     * @param amount (加、减)多少天
     * @return 计算后的日期对象
     */
    public static java.sql.Date getNewDate(java.sql.Date date , long amount){
        long mills = date.getTime();
        mills += amount*24*60*60*1000;
        java.sql.Date backDate = new java.sql.Date(mills);
        return backDate;
    }

    /**
     * 计算两个时间差
     * @param date01
     * @param date02
     * @return
     */
    public static int getSpan(Date  date01,Date date02){
        long span = date01.getTime()-date02.getTime();
        int day = (int)(span/1000/60/60/24);
        return Math.abs(day);

    }


        public static int daysBetween(String startdate) throws ParseException {
    //        String startdate="2010-2-9";
            Date date=new Date();
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            String dateNowStr = sdf2.format(date);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(startdate));

            long start = calendar.getTimeInMillis();
            calendar.setTime(sdf.parse(dateNowStr));


            long end = calendar.getTimeInMillis();
            long betweendays=(end-start)/(1000*3600*24);
            int days = Integer.parseInt(String.valueOf(betweendays));
            return days;
        }



    public static void main(String[] args) {
        System.out.println(getImageName());

        java.sql.Date  date01 =  java.sql.Date.valueOf("2021-10-01");
        java.sql.Date  date02 = getNewDate(date01,31);
        System.out.println(date01+","+date02);





    }

}
